package com.da.lon.wang.xlg.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.ResultSearchActivity;
import com.da.lon.wang.xlg.adapter.ClassyGridAdapter;
import com.da.lon.wang.xlg.adapter.ClassyTypeTypeAdapter;
import com.da.lon.wang.xlg.bean.ClassyTypeBean;
import com.da.lon.wang.xlg.bean.ClassyTypeBean.ClassyGoods;
import com.da.lon.wang.xlg.bean.ClassyTypeBean.ClassyType;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshGridView;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;

public class ClassyTypeFragment extends BaseFragment {
    ListView types;
    PullToRefreshGridView gridView;
    ArrayList<ClassyType> datas;
    ArrayList<ClassyGoods> goods;
    ClassyGridAdapter gridAdapter;
    ClassyTypeTypeAdapter typeAdapter;
    boolean flg = true;
    LinearLayout type_content;
    ImageView type_frush_img;
    View view;

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup var2) {
        view = inflater.inflate(R.layout.ocoffee_classy_type_fragment, null);
        return view;
    }

    @Override
    protected void initView() {
        registerBaseReceiver();
        datas = new ArrayList<ClassyType>();
        types = (ListView) view.findViewById(R.id.types);
        type_frush_img = (ImageView) view.findViewById(R.id.frush_img);
        type_content = (LinearLayout) view.findViewById(R.id.type_content);
        type_frush_img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                getData();
            }
        });
        types.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                showInfoByType(position);
            }
        });
        gridView = (PullToRefreshGridView) view
                .findViewById(R.id.classy_type_grid);
        getData();
    }

    private void getData() {
        try {
            if (!MyApplication.isNetworkAvailable(getActivity())) {
                showDialog();
                type_content.setVisibility(View.GONE);
                type_frush_img.setVisibility(View.VISIBLE);
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                            dismissDialog();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            } else {
                HttpLoader<ClassyTypeBean> loader = new HttpLoader<ClassyTypeBean>(
                        getActivity(), XLGConstants.URL + "Product-category",
                        ClassyTypeBean.class);
                AjaxParams params = new AjaxParams();
                params.put("token", XLGDataUtils.encrypt2MD5("Product"
                        + "2015#$%" + "category"));
                loader.setManagerListener(new OnWebLoadListener<ClassyTypeBean>() {

                    @Override
                    public void OnStart() {
                        showDialog();
                    }

                    @Override
                    public void OnError(String error) {
                        type_content.setVisibility(View.GONE);
                        type_frush_img.setVisibility(View.VISIBLE);
                        dismissDialog();
                    }

                    @Override
                    public void onSuccess(ClassyTypeBean t) {
                        dismissDialog();
                        try {
                            if (t != null && "000000".equals(t.getCode())) {
                                type_content.setVisibility(View.VISIBLE);
                                type_frush_img.setVisibility(View.GONE);
                                datas = t.getResult();
                                if (datas != null && types != null) {
                                    typeAdapter = new ClassyTypeTypeAdapter(
                                            datas, getActivity());
                                    types.setAdapter(typeAdapter);
                                    showInfoByType(0);
                                }
                            } else {
                                type_content.setVisibility(View.GONE);
                                type_frush_img.setVisibility(View.VISIBLE);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                loader.loadData(params);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据类型显示内容
     */
    private void showInfoByType(int position) {
        try {
            typeAdapter.setSelectedItem(position);
            typeAdapter.notifyDataSetInvalidated();
            goods = datas.get(position).getCate();
            gridAdapter = new ClassyGridAdapter(goods, getActivity());
            final ArrayList<ClassyGoods> list = datas.get(position).getCate();
            gridView.getRefreshableView().setAdapter(gridAdapter);
            gridView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    if (!MyApplication.isNetworkAvailable(getActivity())) {
                        return;
                    }
                    if (flg) {
                        PreferenceUtils.getInstance().SetSettingString(
                                PreferencesConfig.SEARCH_SATTR, "");
                        Intent intent = new Intent(getActivity(),
                                ResultSearchActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("cid", list.get(position).getId());
                        intent.putExtras(bundle);
                        startActivity(intent);
                        flg = false;
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onResume() {
        flg = true;
        super.onResume();
        MobclickAgent.onPageStart("ClassyTypeFragment");
        MobclickAgent.onResume(getActivity());
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("ClassyTypeFragment");
        MobclickAgent.onPause(getActivity());
    }

    @Override
    protected void RefreshData() {
        // TODO Auto-generated method stub
        getData();
    }

}
